"use strict";
/* Controllers */
app.controller("permissionRoleController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'permissionRoleService', function ($scope, $compile, $location, $rootScope, $routeParams, permissionRoleService) {

    var vm = $scope.vm;
    vm.p = {};
    vm.p.functionLabel = $routeParams.label;

    vm.result = {};
    var errorFn = function (result) {
        alert(result);
    };

    $scope.listPermissionRole = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        permissionRoleService.listPermissionRole(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'createTime');

        $scope.listPermissionRole();
    };

    $scope.initializeController();


    $scope.confirmDel = function (id, index) {
        vm.toDelId = id
        vm.toDelIndex = index
        $('#myModal').modal('show')
    }

    $scope.ok = function () {
        var vm = $scope.vm;
        console.log("toDelId" + vm.toDelId)
        console.log("toDelIndex" + vm.toDelIndex)
        var params = {};
        params.id = vm.toDelId
        var procFn = function (response) {
            if (response.success && response.data) {
                $('#list_table').bootstrapTable('remove', {
                    field: 'id',
                    values: [vm.toDelId]
                });
                alert("删除成功！")
            } else {
                alert(response.msg);
            }
        };
        permissionRoleService.delPermissionRole(params, procFn, errorFn);
        $('#myModal').modal('hide')
    }

}]);


/* Services */
app.service('permissionRoleService', ['ajaxService', function (ajaxService) {

    this.listPermissionRole = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/permission/listPermissionRole.do", successFunction, errorFunction);
    };

    this.delPermissionRole = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/permission/delPermissionRole.do", successFunction, errorFunction);
    };

}]);

function permissionRoleOptFormat(value, row, index) {
    var optCont = '<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel(' + row.id + ',' + index + ') >删除</a>\n'
    var optCont2 = '<a type="button" class="btn btn-xs btn-primary" href="#/star/user/role_config/id/' + row.id + '" target="_blank">修改</a>\n'
    return [
        optCont2,
        optCont
    ].join('');
}

function permissionTypeFmt(value, row, index) {
    if (value == 0) {
        return '页面'
    } else {
        return '接口'
    }
}

function functionNameFmt(value, row, index) {
    if (value == null) {
        value = '-'
    }
    return value + '（' + row.functionLabels + ')';
}

function actionFmt(value, row, index) {
    if (value == 0 | value == 1) {
        return '有效'
    } else {
        return '<font color=red>无效</font>'
    }
}

function userNameFmt(value, row, index) {
    if (value == null) {
        value = '-'
    }
    if(row.roleName=='用户') {
        return value + '（' + row.userIds + ')';
    }
    return row.userGroupName + '（' + row.userGroupIds + ')';
}


